<?php

namespace app\admin\model;

use app\common\model\TimeModel;
use ClassLibrary\ClHttp;

/**
 * Class UserInfo
 * @package app\admin\model
 * User : Orange Tel: 181-3414-1353 QQ:1139552211
 * Date    : 2021/6/9 上午11:57
 */
class UserInfo extends TimeModel {
    /*
     * BTC状态码
     */
    const COIN_BTC = 1;
    /*
     * ETH状态码
     */
    const COIN_ETH = 2;
    /*
     * Chia状态码
     */
    const COIN_XCH = 3;
    /*
     * Fil状态码
     */
    const COIN_FIL = 4;
    /*
     * Swarm状态码
     */
    const COIN_BZZ = 5;
    /*
     * USDT状态码
     */
    const COIN_USDT = 6;

    protected $name = "user_info";

    protected $deleteTime = "delete_time";


    /**
     *根据用户ID获取指定资产
     * @param $user_id
     * @param $type
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * User    : QingYang
     * Tel     : 181-3414-1353
     * QQ      : 1139552211
     * Date    : 2021/6/3 下午4:54
     */
    public function getCoinByUid($user_id, $type = self::COIN_USDT) {
        return self::instance()->where([
            'uid'  => $user_id,
            'type' => $type
        ])->field(['id,uid,type,assets,frozen'])->find();
    }

    /**
     * 扣钱指定用户流水
     * @param $user_id
     * @param $money
     */
    public function decUserMoney($user_id, $money) {
        UserInfo::instance()->where([
            ['uid', '=', $user_id],
            ['type', '=', UserInfo::COIN_USDT],
            ['assets', '>=', $money]
        ])->dec('assets', $money)->update();
    }

    /**
     * 产出并写入流水
     * @param $list
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/10 上午11:00
     */
    public function coinProduction($list) {
        foreach ($list as $key => $value) {
            UserInfo::instance()->where(['uid' => $value['uid']])->inc('assets', $value['produce_type'])->update();
            $amount_log[] = [
                'uid'         => $value['uid'],
                'type'        => $this->matchOutputCoinLogType($value['produce_type']),
                'assets'      => $value['money'],
                'cointype'        => $value['produce_type'],
                'create_time' => time(),
            ];
        }
        UserAmountLog::instance()->insertAll($amount_log);//写入流水
    }

    /**
     * 匹配产出流水状态码
     * @param $type
     * @return int
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/10 上午10:56
     */
    public function matchOutputCoinLogType($type) {
        switch ($type) {
            case 1:
                $type = 12;
                break;
            case 2:
                $type = 13;
                break;
            case 3:
                $type = 14;
                break;
            case 4:
                $type = 15;
                break;
            case 5:
                $type = 16;
                break;
            case 6:
                $type = 17;
                break;
        }
        return $type;
    }

    /**
     * 返佣并写入流水
     * @param $list
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/10 下午12:00
     */
    public function userRebate($list) {
        foreach ($list as $key => $value) {
            //返佣
            self::instance()->where([
                'uid'  => $value['id'],
                'type' => $value['type']
            ])->inc('assets', $value['money'])->update();
            $amount_log[] = [
                'uid'         => $value['uid'],
                'type'        => $this->matchRebateCoinLogType($value['produce_type']),
                'assets'      => $value['money'],
                'create_time' => time(),
            ];
        }
    }

    /**
     * 匹配返佣状态码
     * @param $type
     * @return int
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/10 上午11:59
     */
    public function matchRebateCoinLogType($type) {
        switch ($type) {
            case 1:
                $type = 2;
                break;
            case 2:
                $type = 3;
                break;
            case 3:
                $type = 5;
                break;
            case 4:
                $type = 4;
                break;
            case 5:
                $type = 18;
                break;
            case 6:
                $type = 1;
                break;
        }
        return $type;
    }

    /**
     * @param $price 美元
     */
    public static function getUsdtByCny($price)
    {
        $eqw = ClHttp::request('https://api.coinbase.com/v2/exchange-rates?currency=USDT','','json');
        return $eqw['data']['rates']['CNY'] * $price;
    }

}